package Controls;

import Core.LayoutManager;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

public class Group extends LayoutManager
{
	private Text capt;
	private int captBorder = 20;
	public Font font = Font.getDefaultFont();
	private LayoutManager owner = null;

	public Group(LayoutManager owner)
	{
		super(owner.getAvailableW(), 0);
		v_border = h_border = 3;
		type = VERTICAL;
		scrollEnable = true;
		owner.addControl(this);

		this.owner = owner;
	}

	public Group(LayoutManager owner, String c)
	{
		super(owner.getAvailableW(), 0);
		v_border = h_border = 3;
		type = VERTICAL;
		scrollEnable = true;
		owner.addControl(this);
		setCaption(c);

		this.owner = owner;
	}

	public void draw(Graphics g, int x, int y)
	{
		int sqx = 5;
		int sqy = font.getHeight()/2-3;

		if(getActiveControl() != null)
		{
			//updateDimensions();

			H = getMinH();

			if(H > owner.getAvailableH() - font.getHeight()) H = owner.getAvailableH() - font.getHeight();

			g.setColor(0xffffff);
			g.drawRect(x+1, y+1+font.getHeight()/2, getW()-3, getH()-2-font.getHeight()/2);

			capt.draw(g, x+captBorder, y+1);

			g.setColor(0x6e6e6e);
			g.fillRect(x+sqx, y+sqy, 8, 8);

			g.setColor(0xffffff);
			g.drawRect(x+sqx, y+sqy, 8, 8);

			g.setColor(0xffffff);
			g.drawLine(x+sqx+4, y+sqy+2, x+sqx+4, y+sqy+6);

			y += font.getHeight();
			H -= spacing;
			additionalHeight = font.getHeight();
			super.draw(g, x, y);
			H += spacing;
		}
		else
		{
			g.setColor(0xffffff);
			g.drawLine(x+1+captBorder, y+1+font.getHeight()/2, x+getW()-h_border*2, y+1+font.getHeight()/2);

			capt.draw(g, x+captBorder, y+1);

			g.setColor(0x6e6e6e);
			g.fillRect(x+sqx, y+sqy, 8, 8);

			g.setColor(0xffffff);
			g.drawRect(x+sqx, y+sqy, 8, 8);

			g.setColor(0xffffff);
			g.drawLine(x+sqx+2, y+sqy+4, x+sqx+6, y+sqy+4);
		}
	}

	public void setCaption(String caption)
	{
		super.setCaption(caption);
		capt = new Text(caption, getW() - captBorder, false);
		capt.bgcolor = 0x6e6e6e;
	}

	public int getH()
	{
		if(getActiveControl() != null)
		{
			//System.out.println(caption + ": full");
			return super.getH() + font.getHeight();
		}
		else
		{
			//System.out.println(caption + ": hide");
			return font.getHeight();
		}
	}


}
